tibco.admin.Main.loadResource("tibco/admin/deployment/sa/topicDef/jss/topicDefJSS.xml", "tibco.admin", "topDefJSS", "ljss", false);

/**
 * Provide definition of SubVar class.
 * This class is the handler of Substitution Variables
 */
jsx3.lang.Class.defineClass(
        "tibco.admin.deployment.sa.topicDef.TopicDef",
        jsx3.lang.Object,
        [tibco.admin.deployment.sa.ServiceAssemblyInterface],
        function(plugin) {

            plugin.prototype.serviceAssemblyEntityID = null;
            plugin.prototype.latestSavedXMLString = "";
            // use to check whether data is changed after reading from server or last being saved.

            plugin.prototype.getButtonText = function() {
                return tibco.admin.Main.getServer().getDynamicProperty("@@lblSectionTitle_46");
                ;
            }

            plugin.prototype.getNamespace = function() {
                return "tibco.admin";
            }

            plugin.getInstance = function() {
                if(!this.instance) {
                    this.instance = new tibco.admin.deployment.sa.topicDef.TopicDef();
                }
                return this.instance;
            }

            plugin.prototype.onAfterComponentUnloaded = function() {
            }

            plugin.prototype.loadComponent = function(parent) {
                var me = this;
                tibco.admin.Main.loadUIResourceAsync(parent, this.getNamespace(), "tibco/admin/deployment/sa/topicDef/ui/topicDef.xml", me, me.componentLoaded);
            }

            //call this method to begin the service call (eg.service.callgetTopicDefinitions();)
            plugin.prototype.callgetTopicDefinitions = function(objEvent) {
                if(this.isInEditMode()) {
                    return;
                }
                if(!objEvent.entityID) return;
                // the service assembly entityID for current list.
                this.serviceAssemblyEntityID = objEvent.entityID;

                var objService = tibco.admin.Main.loadService("tibco.admin",
                        "tibco/admin/deployment/sa/topicDef/rules/getTopicDefinitions.xml",
                        "getTopicDefinitions");
                objService.setEndpointURL(tibco.admin.util.ServiceHelper.getDeploymentEndpointURL());

                var me = this;
                objService.serviceAssemblyEntityID = this.serviceAssemblyEntityID;
                objService.subscribe(jsx3.net.Service.ON_SUCCESS, me, me.ongetTopicDefinitionsSuccess);
                objService.doCall();
            };

            plugin.prototype.ongetTopicDefinitionsSuccess = function(objEvent) {
                this.block.getDescendantOfName("mtxTopicDefinitions_46").repaintData();
                this.latestSavedXMLString = this.block.getDescendantOfName("mtxTopicDefinitions_46").getXML().toString();
            };

            //call this method to begin the service call (eg.service.callupdateTopicDefinition();)
            plugin.prototype.callupdateTopicDefinition = function() {
                var objService = tibco.admin.Main.loadService("tibco.admin",
                        "tibco/admin/deployment/sa/topicDef/rules/updateTopicDefinition.xml",
                        "updateTopicDefinition");
                objService.setEndpointURL(tibco.admin.util.ServiceHelper.getDeploymentEndpointURL());

                objService.serviceAssemblyEntityID = this.serviceAssemblyEntityID;
                var me = this;
                //subscribe and call
                objService.subscribe(jsx3.net.Service.ON_SUCCESS, me, me.onupdateTopicDefinitionSuccess);
                objService.doCall();
            };

            plugin.prototype.onupdateTopicDefinitionSuccess = function(objEvent) {
                this.showEditMode(false);
                this.endEditMode();
                this.latestSavedXMLString = this.block.getDescendantOfName("mtxTopicDefinitions_46").getXML().toString();
            };

            plugin.prototype.showEditMode = function(editMode) {
                var server = tibco.admin.Main.getServer();
                if(editMode) { // show Save and Cancel buttons, add TextBox
                    this.block.getDescendantOfName("btnEdit_46").setDisplay(jsx3.gui.Block.DISPLAYNONE, true);
                    this.block.getDescendantOfName("btnSave_46").setDisplay(jsx3.gui.Block.DISPLAYBLOCK, true);
                    this.block.getDescendantOfName("btnCancel_46").setDisplay(jsx3.gui.Block.DISPLAYBLOCK, true);

                    if(this.block.getDescendantOfName("mcPhysicalName_46").getChildren().length == 0) {
                        this.block.getDescendantOfName("mcPhysicalName_46").setChild(new jsx3.gui.TextBox());
                        this.block.getDescendantOfName("mtxTopicDefinitions_46").repaint();
                    }
                }
                else {  // show Edit buttons, remove TextBox
                    this.block.getDescendantOfName("btnEdit_46").setDisplay(jsx3.gui.Block.DISPLAYBLOCK, true);
                    this.block.getDescendantOfName("btnSave_46").setDisplay(jsx3.gui.Block.DISPLAYNONE, true);
                    this.block.getDescendantOfName("btnCancel_46").setDisplay(jsx3.gui.Block.DISPLAYNONE, true);
                    this.block.repaint();

                    if(this.block.getDescendantOfName("mcPhysicalName_46").getChildren().length > 0) {
                        this.block.getDescendantOfName("mcPhysicalName_46").removeChild(0);
                        this.block.getDescendantOfName("mtxTopicDefinitions_46").repaint();
                    }
                }

            }

            plugin.prototype.onEdit = function() {
                this.showEditMode(true);
                this.beginEditMode();
            };

            plugin.prototype.onSave = function() {
                // save the data;
                this.callupdateTopicDefinition();
            }

            plugin.prototype.onCancel = function() {
                if(tibco.admin.util.MatrixHelper.isDataEqual(this.latestSavedXMLString, this.block.getDescendantOfName("mtxTopicDefinitions_46").getXML().toString())) { // no change
                    this.showEditMode(false);
                    this.endEditMode();
                }
                else {
                    var me = this;
                    tibco.admin.Main.confirm(
                            tibco.admin.Main.getServer().getDynamicProperty("@ConfirmDiscardTitle_46"),
                            tibco.admin.Main.getServer().getDynamicProperty("@ConfirmDiscardMessage_46"),
                            function(objDialog) {
                                objDialog.doClose();
                                me.showEditMode(false);
                                me.endEditMode();
                                me.block.getDescendantOfName("mtxTopicDefinitions_46").getXML().loadXML(me.latestSavedXMLString);
                                me.block.getDescendantOfName("mtxTopicDefinitions_46").repaint();
                            },
                            function(objDialog) {  // do nothing yet
                                objDialog.doClose();
                            })
                }
            }

            plugin.prototype.subscribeAll = function() {
                tibco.admin.PubSub.subscribe(tibco.admin.deployment.saList.EventID.SERVICEASSEMBLY_ROW_SELECTED, this, this.callgetTopicDefinitions);
            }

            plugin.prototype.unsubscribeAll = function() {
                tibco.admin.PubSub.unsubscribe(tibco.admin.deployment.saList.EventID.SERVICEASSEMBLY_ROW_SELECTED, this);
            }

            plugin.prototype.componentLoaded = function(objEvent) {
                if(objEvent.status == "SUCCESS") {
                    var me = this;
                    this.block = objEvent.obj;
                    this.block.handler = me;
                    this.subscribeAll()
                }
                else {
                    if(objEvent.exception)
                    {
                        tibco.admin.Main.logException(objEvent.exception, objEvent.message);
                    }
                    else {
                        jsx3.log(objEvent.message);
                    }
                }
            }

            plugin.prototype.onDestroyPlugin = function() {
                this.unsubscribeAll();
                this.block = null;
            }

            plugin.prototype.getComponentPath = function() {
                return  "tibco/admin/deployment/sa/topicDef/component.xml"
            }
        }
        );

